package com.sdjzu.wangfuying.disableautobrightness.util;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.RequiresApi;

public class PermissionUtil {
    @RequiresApi(api = Build.VERSION_CODES.M)
    public static boolean isUsageStatsPermissionGranted(Context context) {
        AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
        int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName());
        if (mode == AppOpsManager.MODE_DEFAULT) {
            return context.checkCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) ==
                    PackageManager.PERMISSION_GRANTED;
        } else {
            return mode == AppOpsManager.MODE_ALLOWED;
        }
    }
}
